package com.chucang.shucang.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chucang.shucang.common.base.utils.R;
import com.chucang.shucang.common.security.annotation.Inner;
import com.chucang.shucang.pms.constant.PmsCommonConstant;
import com.chucang.shucang.pms.entity.BlindBoxBasicPlayEntity;
import com.chucang.shucang.pms.entity.CollectionBaseEntity;
import com.chucang.shucang.pms.service.BlindBoxBasicPlayService;
import com.chucang.shucang.pms.service.CollectionBaseService;
import com.chucang.shucang.pms.vo.BlindBoxBasicOpenResVO;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 盲盒基础玩法
 *
 * @author flitsneak flitsneak@gmail.com
 * @since 1.0.0 2022-09-12
 */
@RestController
@RequestMapping("/blindboxbasicplay")
@RequiredArgsConstructor
@Api(tags = "盲盒基础玩法")
public class BlindBoxBasicPlayController {

    private final BlindBoxBasicPlayService blindBoxBasicPlayService;
    private final CollectionBaseService collectionBaseService;

    @Inner
    @GetMapping("/openBlindBox/{id}/{serialNum}")
    public R<BlindBoxBasicOpenResVO> openBlindBox(@PathVariable Long id, @PathVariable Integer serialNum) {
        LambdaQueryWrapper<BlindBoxBasicPlayEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BlindBoxBasicPlayEntity::getBbId, id)
                .eq(BlindBoxBasicPlayEntity::getBbSerialNum, serialNum)
                .select();
        BlindBoxBasicPlayEntity one = blindBoxBasicPlayService.getOne(lambdaQueryWrapper);
        if (Objects.isNull(one)) {
            return R.failed();
        }
        LambdaUpdateWrapper<BlindBoxBasicPlayEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(BlindBoxBasicPlayEntity::getId, one.getId())
                .set(BlindBoxBasicPlayEntity::getStatus, PmsCommonConstant.BLIND_BOX_BASIC_OPEN);
        blindBoxBasicPlayService.update(lambdaUpdateWrapper);
        //返回必要信息
        CollectionBaseEntity baseEntity = collectionBaseService.getById(one.getCollectionId());
        BlindBoxBasicOpenResVO blindBoxBasicOpenResVO = new BlindBoxBasicOpenResVO();
        blindBoxBasicOpenResVO.setId(baseEntity.getId());
        blindBoxBasicOpenResVO.setSerialNum(one.getDcSerialNum());
        blindBoxBasicOpenResVO.setDcName(baseEntity.getDcName());
        blindBoxBasicOpenResVO.setDcDisplay(baseEntity.getDcDisplay());
        blindBoxBasicOpenResVO.setCollectionSerialNum(String.format(PmsCommonConstant.SERIAL_NUM, one.getDcSerialNum(), baseEntity.getTotalNum()));
        return R.ok(blindBoxBasicOpenResVO);
    }
}